package com.fourforfo.fourmall.seckill.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fourforfo.common.utils.R;
import com.fourforfo.fourmall.seckill.feign.CouponFeignService;
import com.fourforfo.fourmall.seckill.feign.ProductFeignService;
import com.fourforfo.fourmall.seckill.service.SeckillService;
import com.fourforfo.fourmall.seckill.to.SeckillSessionWithSkusTO;
import com.fourforfo.fourmall.seckill.to.SeckillSkuRedisTO;
import com.fourforfo.fourmall.seckill.to.SkuInfoTO;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @program: xxl-sso-core
 * @description: 秒杀商品定时上架计时器
 *                  每天晚上3点，上架最近三天需要秒杀的商品
 *                  当天：00:00:00 - 23:59:59
 *                  明天：00:00:00 - 23:59:59
 *                  后天：00:00:00 - 23:59:59
 * @author: fourforfo
 * @email: fourforfo@gmail.com
 * @create: 2022-09-14 13:38
 **/

@Slf4j
@Service
public class SeckillSkuScheduled {

    @Autowired
    SeckillService seckillService;


    @Autowired
    RedissonClient redissonClient;

    private final static String SECKILL_UPLOAD_LOCK = "seckill:upload:lock";

    @Async
//    @Scheduled(cron="0 0 3 * * ?")
    @Scheduled(cron="0 0 3 * * ?")
    public void uploadSeckillSkuLatest3Days(){

        RLock lock = redissonClient.getLock(SECKILL_UPLOAD_LOCK);
        lock.lock();
        log.warn("定时器开始执行,获得分布式锁！");
        try {
            seckillService.uploadSeckillSkuLatest3Days();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

        log.warn("定时任务执行完毕，分布式锁释放！");
    }



}
